module.exports = class extends think.Model {
  /// 获取任务回复详情
  async info (id) {
    let _id = think.objId(id)
    /// 获取任务回复
    const task_result_db = think.mongoose('mongo/task_result')
    let task_result = await task_result_db.findOne({_id})
    /// 获取关联用户
    const users_db = think.mongoose('mongo/users')
    let users = await users_db.findOne({_id: think.objId(task_result.uid)})
    /// 获取关联任务
    const task_db = think.mongoose('mongo/task')
    let task = await task_db.findOne({_id: think.objId(task_result.tid)})

    
    let result = {
      result: task_result,
      users, task
    }

    return result
  }
  /// 创建任务回复
  async create (uid, tid) {
    const db = think.mongoose('mongo/task_result')
    let doc = new db({tid, uid})
    await doc.save()
    let get_num = await this.count({tid})
    const task_db = think.mongoose('mongo/task')
    await task_db.updateOne({
      _id: think.objId(tid)
    }, { get_num }) 
  }
  /// 完成任务时提交的回复
  async finish (id, text, picture) {
    let _id = think.objId(id)
    const db = think.mongoose('mongo/task_result')
    await db.updateOne({_id}, {
      text,
      picture,
      status: 1, // 设置任务回复状态为任务完成
      update_at: Date.now(),
    })
  }
  /// 审核任务回复
  async audit (id, status, reason) {
    let _id = think.objId(id)
    const db = think.mongoose('mongo/task_result')
    await db.updateOne({_id}, {
      status,
      reason,
      update_at: Date.now(),
    })
  }
  /// 获取任务相关的回复 
  async get (tid) {
    const db = think.mongoose('mongo/task_result')
    let result = await db.find({tid})
    return result
  }
  /// 获取任务回复列表 
  async list (page, num, json) {
    page = parseInt(page || 1)
    num = parseInt(num || 15)
    json = json || {}
    /// 获取任务回复列表
    const task_result_db = think.mongoose('mongo/task_result')
    let task_result_list = await task_result_db.find(json).sort({_id: -1}).skip((page-1) * num).limit(num) // 获取列表
    let count = await task_result_db.find(json).count()// 获取总数
    /// 循环所有的用户和任务ID
    let uidList = [], tidList = []
    task_result_list.forEach(item => {
      let uid = think.objId(item.uid), tid = think.objId(item.tid)
      uidList.indexOf(uid) == -1 && uidList.push(uid)
      tidList.indexOf(tid) == -1 && tidList.push(tid)
    })
    /// 获取用户列表
    const users_db = think.mongoose('mongo/users')
    let users = await users_db.find({
      _id:{ $in: uidList }
    })
    /// 获取任务列表
    const task_db = think.mongoose('mongo/task')
    let tasks = await task_db.find({
      _id:{ $in: tidList }
    })
    /// 返回列表信息
    return {
      result: task_result_list, /// 任务回复列表
      users, // 关联的用户列表
      tasks, // 关联的任务列表
      page: { /// 分页信息
        page, num, count
      }
    }
  }
  async count (json) {
    const task_result_db = think.mongoose('mongo/task_result')
    let count = await task_result_db.find(json).count() // 获取列表
    return count
  }
  // 获取对应用户任务的任务回复
  async getUserResult (uid, tid) {
    const db = think.mongoose('mongo/task_result')
    let task = await db.findOne({ uid, tid })
    return task
  }
};
